#include "udpsample.h"
#include <QDebug>

UdpSample::UdpSample(QObject *parent) : QObject(parent)
{
    connect(&udpSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}

bool UdpSample::bind(quint16 port)
{
    return udpSocket.bind(QHostAddress::Any, port);
}

qint64 UdpSample::writeDatagram(QString ip, quint16 port, QString msg)
{
    return  udpSocket.writeDatagram(msg.toUtf8(), QHostAddress(ip), port);
}

void UdpSample::onReadyRead()
{
    QHostAddress addr;
    quint16 port;

    char buff[1024];
    qint64 r = udpSocket.readDatagram(buff, 1024, &addr, &port);
    qDebug() << r;
    QString msg = QString::fromUtf8(buff, static_cast<int>(r));
    qDebug() << msg;

    qDebug() << addr << port;
}
